﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ZhimeiSoft.Framework.Client.Base;
using ZhimeiSoft.CharmManager.DOL;
using ZhimeiSoft.Framework.Client.Utils;
using ZhimeiSoft.CharmManager.Client.Manager;
using ZhimeiSoft.CharmManager.Client.Interface;


namespace ZhimeiSoft.CharmManager.Client.Forms.Control.ToDoList
{
    public partial class ToDoListControl : BaseUserControl
    {
        private DateTime currQueryTime = DateTime.Now;
        /// <summary>
        /// 待办事项
        /// </summary>
        private DataSet dsTodoListUnfiniushed = new DataSet();
        /// <summary>
        /// 已完结
        /// </summary>
        private DataSet dsTodoListFinush = new DataSet();
        /// <summary>
        /// 超期的待办事项
        /// </summary>
        private DataSet dsTodoListUnDate = new DataSet();
        
        public ToDoListControl()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 初始化控件并刷新数据
        /// </summary>
        public void InitControl(DateTime queryStartTime)
        {
            string error = string.Empty;
            currQueryTime = new DateTime(queryStartTime.Ticks);
            
            #region 进行中的待办事项包括（未开始状态、和正在进行状态的）
            if (!BussinessProxyManager.Instance.GetStateToDoList(queryStartTime, string.Empty, out dsTodoListUnfiniushed, out error))
            {
                MsgBox.MsgInfo("初始化代办事项错误：" + error);
                return;
            }
            dsTodoListFinush = dsTodoListUnfiniushed.Clone();
            dsTodoListUnDate = dsTodoListUnfiniushed.Clone(); ;

            gvToDoList.BeginUpdate();
            try
            {
                string filter = DBEntityToDoList.FieldName.TODOSTATE + " = '" + DBEntityToDoList.ENUM_TODOSTATE.T0V + "' OR "
                    + DBEntityToDoList.FieldName.TODOSTATE + " = '" + DBEntityToDoList.ENUM_TODOSTATE.T0T + "'";
                DataRow[] drTodoList = dsTodoListUnfiniushed.Tables[0].Select(filter);
                foreach (DataRow dr in drTodoList)
                {//删除已经完成的，和过期的
                    if (dr[DBEntityToDoList.FieldName.TODOSTATE].ToString().Equals(DBEntityToDoList.ENUM_TODOSTATE.T0V))
                    {
                        //已经完成
                        dsTodoListFinush.Tables[0].ImportRow(dr);
                    }
                    else
                    {
                        //超期的
                        dsTodoListUnDate.Tables[0].ImportRow(dr);
                    }
                    dr.Delete();
                }

                dsTodoListUnfiniushed.AcceptChanges();
                dsTodoListUnDate.AcceptChanges();
                dsTodoListFinush.AcceptChanges();
                gridToDoList.DataSource = dsTodoListUnfiniushed.Tables[0];
            }
            finally
            {
                gvToDoList.EndUpdate();
            }
            #endregion

            #region 过期的代办事项
            gvToDoListUnFinush.BeginUpdate();
            try
            {
                gridToDoListUnFinush.DataSource = dsTodoListUnDate.Tables[0];
            }
            finally
            {
                gvToDoListUnFinush.EndUpdate();
            }
            #endregion

            #region 已经完成的待办事项
            try
            {
                gvToDoListFinush.BeginUpdate();
                gridToDoListFinush.DataSource = dsTodoListFinush.Tables[0];
            
            }
            finally
            {
                gvToDoListFinush.EndUpdate();
            }
            #endregion

        }

        private void gvToDoList_DoubleClick(object sender, EventArgs e)
        {
            if (gvToDoList.FocusedRowHandle < 0)
                return;

            string error = string.Empty;
            DataSet dsEmployee = new DataSet();
            if(!GetEmployeeInfo(out dsEmployee, out error)){
                MsgBox.MsgInfo("获取员工数据错误：" + error);
                return;
            }
            DataRow drToDoList = gvToDoList.GetDataRow(gvToDoList.FocusedRowHandle);
            DBEntityToDoList todoListInfo = new DBEntityToDoList(drToDoList);
            frmToDoListInfo frmlistInfo = new frmToDoListInfo(dsEmployee, todoListInfo);
            if (frmlistInfo.ShowDialog() == DialogResult.OK)
            {
                this.InitControl(currQueryTime);
            }

        }
        private bool GetEmployeeInfo(out DataSet dsEmployee, out string error)
        {
            return BussinessProxyManager.Instance.QueryEmployeeState(string.Empty, out dsEmployee, out error);
            
        }
    }
}
